package com.atguigu.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author lzc
 * @Date 2023/3/11 11:42
 */
public class AtguiguUtil {
    private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    public static String tsToDate(Long ts) {
        //return new SimpleDateFormat("yyyy-MM-dd").format(ts);  // 线程安全问题
        return dateFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault()));
    }
    
    
    public static Set<String> ikSpilt(String s) {
        HashSet<String> result = new HashSet<>();
        // 内存流
        // 字符串 -> 字符输入流
        StringReader reader = new StringReader(s);
        IKSegmenter segmenter = new IKSegmenter(reader, true);// 创建一个IKSegmenter
        Lexeme next = null;
        try {
            next = segmenter.next();
            while (next != null) {
                String kw = next.getLexemeText();
                result.add(kw);
                next = segmenter.next();
            }
    
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
        return result;
    }
    
    public static void main(String[] args) throws IOException {
        for (String a : ikSpilt("我是中国人")) {
            System.out.println(a);
        }
    }
    
    public static String tsToDateTime(long ts) {
        return dateTimeFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault()));
    }
    
    public static Long dateToTs(String date) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    
}
